package luyangzuoye.zuoye4.demos.web.service;

import luyangzuoye.zuoye4.demos.web.Mapper.BookRepository;
import luyangzuoye.zuoye4.demos.web.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookRepository bookRepository;
    @Override
    public List<Book> findAll(){
        return bookRepository.findAll();}
        @Override
        public Book findById(Long id){
            return bookRepository.getById(id);}
        @Override
            public void save(Book book){
                bookRepository.save(book);}
        @Override
        public void update(Book book){
        bookRepository.save(book);
        }

        @Override
        public void deleteById(Long id){
              bookRepository.deleteById(id);}
        @Override
        public List<Book> findByTitle (String title){
               return bookRepository.findByTitleContains(title.trim());
        }
    @Override
    public String getPublisherNameById(Long id) {
        Book book = bookRepository.getById(id);
        if (book != null) {
            return book.getPublisher().getPublisherName();
        }
        return null;
    }
    @Override
    public List<String> findAuthorList(){
        return bookRepository.getAuthorList();
    }
}